package com.storeroompda.ui.login;

import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import android.util.Log;
import com.storeroompda.base.model.BaseModel;
import com.storeroompda.utils.Utils;
import com.vilyever.socketclient.SocketClient;
import com.vilyever.socketclient.helper.SocketClientDelegate;
import com.vilyever.socketclient.helper.SocketClientSendingDelegate;
import com.vilyever.socketclient.helper.SocketPacket;
import com.vilyever.socketclient.helper.SocketResponsePacket;
import com.vilyever.socketclient.util.CharsetUtil;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginModelImpl extends BaseModel implements LoginModel {
    private String tag = "LoginModelImpl";
    private String msg="";
    SocketClient socketClient = new SocketClient();

    public void postHandle(final CallBack listener, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case -1:
                        listener.onFail(msg);
                        break;
                    case 0:
                        listener.onloginSuccess();
                        break;
                    default:
                        break;
                }
            }
        });
    }

//    @Override
//    public void login(final String username, final String password, final LoginModel.CallBack listener) {
//        try {
//            FormBody.Builder builder = new FormBody.Builder();
//            builder.add("", "");
//            RequestBody formBody = builder.build();
//            final Request request = new Request.Builder().url(Utils.url).post(formBody).build();
//            client.newCall(request).enqueue(new Callback() {
//                @Override
//                public void onFailure(Call call, IOException e) {
//                    msg= Utils.netConnectionError;
//                    postHandle(listener, -1);
//                }
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    if (response.isSuccessful()) {//回调的方法执行在子线程。
//                        String result = response.body().string();
//                        Log.e(tag,result);
//                        bean = gson.fromJson(result, PayRecordListBean.class);
//                        if (null != bean && bean.getRes() == 0 ) {
//                            mlist = bean.getData();
//                            postHandle(listener, 0);
//                        }else {
//                            msg=bean.getMsg();
//                            postHandle(listener, -1);
//                        }
//                    } else {
//                        msg=Utils.netServerError;
//                        postHandle(listener, -1);
//                    }
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//            msg=Utils.exceptionError;
//            postHandle(listener, -1);
//        }
//    }



    @Override
    public void login(final String username, final String password, final LoginModel.CallBack listener) {
        try {
            socketClient.getAddress().setRemoteIP(Utils.url);
            socketClient.getAddress().setRemotePort(8080);
            socketClient.getAddress().setConnectionTimeout(10 * 1000);
            socketClient.setCharsetName(CharsetUtil.UTF_8);
            socketClient.registerSocketClientDelegate(new SocketClientDelegate() {
                @Override
                public void onConnected(SocketClient client) {
                    Log.e(tag,"onConnected");
                }
                @Override
                public void onDisconnected(SocketClient client) {
                    Log.e(tag,"onDisconnected");
                }
                @Override
                public void onResponse(final SocketClient client, @NonNull SocketResponsePacket responsePacket) {
                    Log.e(tag,"onResponse:"+responsePacket.getMessage());
                }
            });
            socketClient.connect();
        }
        catch (Exception e) {
            e.printStackTrace();
            msg=Utils.exceptionError;
            postHandle(listener, -1);
        }
    }

    @Override
    public void test()
    {
        try {
            socketClient.sendData("00".getBytes("UTF-8"));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

